package com.rjwh.dingdong.client.activity.drawbook.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.drawbook.DrawBookCatalogMainActivity;
import com.rjwh.dingdong.client.activity.drawbook.DrawBookDetailActivity;
import com.rjwh.dingdong.client.adapter.DrawBookCatalogListAdapter;
import com.rjwh.dingdong.client.bean.localbean.DrawBookData;
import com.rjwh.dingdong.client.bean.localbean.DrawBookTypeList;
import com.rjwh.dingdong.client.bean.localbean.DrawBookTypeListData;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.mvp.presenter.impl.DrawBookCatalogFragmentPresenter;
import com.rjwh.dingdong.client.mvp.view.DrawBookCatalogFragmentView;
import com.rjwh.dingdong.client.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Kay
 * @ClassName DrawBookCatalogFragment
 * @Description 绘本馆
 * @date 2016-8-16
 * @history 2016-8-16 author: description:
 */
public class DrawBookCatalogFragment extends BaseFragment implements DrawBookCatalogListAdapter.DrawBookCatalogItemClickListener, DrawBookCatalogFragmentView {

    @BindView(R.id.draw_book_catalog_national_list)
    ListView drawBookCatalogNationalList;
    @BindView(R.id.draw_book_catalog_national_mPullRefreshView)
    SwipyRefreshLayout drawBookCatalogNationalMPullRefreshView;

    private DrawBookCatalogListAdapter drawBookCatalogListAdapter;
    private DrawBookCatalogFragmentPresenter presenter;
    private int ageCode = 0;
    private int typeId;
    private boolean isInit;

    public static DrawBookCatalogFragment getInstance(int typeId) {
        DrawBookCatalogFragment fragment = new DrawBookCatalogFragment();
        Bundle args = new Bundle();
        args.putInt("typeId", typeId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            typeId = getArguments().getInt("typeId", 1);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.draw_book_catalog_fragment_layout, container, false);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new DrawBookCatalogFragmentPresenter(getContext(), this);
        if (getUserVisibleHint() && isVisible() && isInit) {
            drawBookCatalogNationalMPullRefreshView.doPullRefreshing();//加载数据的方法
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        //判断是否可见 并且是否已经初始化过，以防空指针
        if (isVisibleToUser && isVisible() && isInit) {
            drawBookCatalogNationalMPullRefreshView.doPullRefreshing();//加载数据的方法
        } else {
            isInit = false;
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isInit = true;//初始化后 将其设置成true  表示已经初始化控件过了
    }

    public void setAgeCode(int ageCode) {
        this.ageCode = ageCode;
    }

    public int getAgeCode() {
        return ageCode;
    }

    public void doRefresh() {
        if (drawBookCatalogNationalMPullRefreshView != null) {
            drawBookCatalogNationalMPullRefreshView.doPullRefreshing();
        }
    }

    private void initView() {
        drawBookCatalogNationalMPullRefreshView.setDirection(SwipyRefreshLayoutDirection.TOP);
        drawBookCatalogNationalMPullRefreshView.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        doGetDrawBookCatalog(ageCode);
                        break;

                    default:
                        break;
                }
            }
        });
        drawBookCatalogListAdapter = new DrawBookCatalogListAdapter(getContext(), this);
        drawBookCatalogNationalList.setAdapter(drawBookCatalogListAdapter);
    }

    private void doGetDrawBookCatalog(int ageCode) {
        //默认传全部
        presenter.doGetDrawBookCatalog(Integer.toString(ageCode), Integer.toString(typeId));
    }

    @Override
    public void onDrawBookCatalogItemClick(DrawBookData bookData) {
        Intent intent = new Intent(getContext(), DrawBookDetailActivity.class);
        String bookid = bookData.getBookid();
        intent.putExtra("bookid", bookid);
        startActivity(intent);
    }


    @Override
    public void setRefreshing(boolean refreshing) {
        drawBookCatalogNationalMPullRefreshView.setRefreshing(refreshing);
    }

    @Override
    public void loadData(List<DrawBookTypeListData> booktypelist) {
        DrawBookCatalogMainActivity.updateAge(true);
        //adapter填充的集合
        List<DrawBookTypeList> newbooklist = new ArrayList<>();
        for (DrawBookTypeListData drawBookTypeListData : booktypelist) {
            List<DrawBookData> booklist = drawBookTypeListData.getBooklist();
            int page = booklist.size() / 3 + (booklist.size() % 3 > 0 ? 1 : 0);

            for (int i = 0; i < page; i++) {
                List<DrawBookData> tempList;
                if (i == page - 1) {
                    tempList = booklist.subList(i * 3, booklist.size());
                } else {
                    tempList = booklist.subList(i * 3, (i + 1) * 3);
                }

                if (page == 1) {
                    newbooklist.add(new DrawBookTypeList(true, true, drawBookTypeListData.getTypeid(), drawBookTypeListData.getTypeimage(), drawBookTypeListData.getTypecode(), tempList));
                } else {
                    if (i == 0) {
                        newbooklist.add(new DrawBookTypeList(true, false, drawBookTypeListData.getTypeid(), drawBookTypeListData.getTypeimage(), drawBookTypeListData.getTypecode(), tempList));
                    } else if (i == page - 1) {
                        newbooklist.add(new DrawBookTypeList(false, true, drawBookTypeListData.getTypeid(), drawBookTypeListData.getTypeimage(), drawBookTypeListData.getTypecode(), tempList));
                    } else {
                        newbooklist.add(new DrawBookTypeList(false, false, drawBookTypeListData.getTypeid(), drawBookTypeListData.getTypeimage(), drawBookTypeListData.getTypecode(), tempList));
                    }
                }

            }
        }
        drawBookCatalogListAdapter.clear();
        drawBookCatalogListAdapter.appendToList(newbooklist);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}


